package com.jackchong.utils;

import android.content.Context;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.utils.R;

public class CommonUtils {


    /**
     * Hides the input method.
     *
     * @param context context
     * @param view    The currently focused view
     * @return success or not.
     */
    public static boolean hideInputMethod(Context context, View view) {
        if (context == null || view == null) {
            return false;
        }

        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            return imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }

        return false;
    }

    /**
     * Show the input method.
     *
     * @param context context
     * @param view    The currently focused view, which would like to receive soft
     *                keyboard input
     * @return success or not.
     */
    public static boolean showInputMethod(Context context, View view) {
        if (context == null || view == null) {
            return false;
        }

        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            return imm.showSoftInput(view, 0);
        }

        return false;
    }


    public static void showToast(Context context, String message) {
        try {
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void showLongToast(Context context, String message) {
        Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }

    public static void showAlert(Context context, String title, String message, String pBtnText, DialogInterface.OnClickListener pClkListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton(pBtnText, pClkListener);
        builder.show();
    }

    public static void showAlert(Context context, String title, String message, String pBtnText, String nBtnText, DialogInterface.OnClickListener pClkListener,
                                 DialogInterface.OnClickListener nClkListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton(pBtnText, pClkListener);
        builder.setNegativeButton(nBtnText, nClkListener);
        builder.show();
    }

    /***
     * 显示提示对话框
     *
     * @param Title
     *            标题
     * @param MSsg
     *            内容
     * @param OKText
     *            确认按钮文字
     * @param OKListener
     *            确认按钮事件处理
     * @param CancelText
     *            取消按钮文字
     * @param CancelListener
     *            取消按钮文字处理
     */
    public static void showAlert(Context context, String Title, String MSsg,
                                 String OKText, DialogInterface.OnClickListener OKListener, String CancelText,
                                 DialogInterface.OnClickListener CancelListener, CharSequence[] items,
                                 boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener,
                                 CharSequence[] sing_items, int checkedItem,
                                 DialogInterface.OnClickListener singLis) {
        AlertDialog.Builder alert = new AlertDialog.Builder(context);
        alert.setCancelable(true);
        if (Title != null) {
            alert.setTitle(Title);
        }
        if (MSsg != null) {
            alert.setMessage(MSsg);
        }
        if (OKListener != null) {
            if (OKText != null) {
                alert.setPositiveButton(OKText, OKListener);
            }
        }
        if (CancelListener != null) {
            if (CancelText != null) {
                CancelText = "取消";
                alert.setNegativeButton(CancelText, CancelListener);
            }
        }
        if (listener != null) {
            alert.setMultiChoiceItems(items, checkedItems, listener);
        }
        if (singLis != null) {
            alert.setSingleChoiceItems(sing_items, checkedItem, singLis);
        }

        alert.create().show();

    }


    private static long lastClickTime;

    public static boolean isFastRepeatClick(long duration) {
        long time = System.currentTimeMillis();
        long deltaTime = time - lastClickTime;
        if (0 < deltaTime && deltaTime < duration) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    /**
     * 当前控件是否支持用户快速点击
     *
     * @param v
     * @return
     */
    public static boolean getSupportFastClick(View v) {
        boolean isSupportFastClick = false;
        try {
            Object temp = v.getTag(R.id.is_support_fast_click);
            if (null != temp) {
                isSupportFastClick = (boolean) temp;
            }
        } catch (Exception e) {

        }
        return isSupportFastClick;
    }

    /**
     * 封装给多个控件设置点击事件
     *
     * @param listener
     * @param views
     */
    public static void setSomeOnClickListeners(View.OnClickListener listener, View... views) {
        if (views == null) {
            return;
        }
        for (View view : views) {
            if (view != null) {
                view.setOnClickListener(listener);
            }
        }
    }

    /**
     * 将整数转化为两位的string
     */
    public static String intTo2String(int num) {
        String result = "";
        if (num < 10) {
            result = "0" + num;
        } else {
            result = num + "";
        }
        return result;
    }

    /**
     * 输入 每三个数字 加一个逗号
     * @param editText
     */
    public static void inputWithCommaListener(final EditText editText) {

        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {

                if (count != before) {
                    String sss = "";
                    String string = s.toString().replace(",", "");
                    int b = string.length() / 3;
                    if (string.length() >= 3) {
                        int yushu = string.length() % 3;
                        if (yushu == 0) {
                            b = string.length() / 3 - 1;
                            yushu = 3;
                        }
                        for (int i = 0; i < b; i++) {
                            sss = sss + string.substring(0, yushu) + "," + string.substring(yushu, 3);
                            string = string.substring(3, string.length());
                        }
                        sss = sss + string;
                        editText.setText(sss);
                    }
                }
                editText.setSelection(editText.getText().length());
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }
}
